<template>
  <div class="login-container" :style="login_background">
    <div class="login-main">
      <div class="main-left">
        <img :src="image_left"></img>
      </div>
      <div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" >
        <div style="font-size:20px;color:#007800;text-align: center;margin-bottom: 30px;font-weight: 600;">
        {{site_name}}代理端
        </div>
        <div class="center">
          <el-form-item prop="username">
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入登录账号"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>
          <el-form-item prop="password">

            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入登录密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
          <el-form-item prop="code">

            <el-input
              ref="code"
              v-model="loginForm.code"
              placeholder="请输入验证码"
              name="code"
              type="number"
              auto-complete="on"

            />
            <span class="show-captcha" >
              <div class="imgs" @click="getCaptcha()"><img :src="captchatImg" style="width: 110px;"/></div>
            </span>
          </el-form-item>
        </div>
        <el-button :loading="loading" type="primary" class="login-btn" @click.native.prevent="handleLogin">立即登录</el-button>
      </el-form>
      </div>
    </div>

  </div>
</template>

<script>
import { login_get,get_captcha } from '@/api/user'
import { title } from '@/settings'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        code: '',
        key:'',
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '登录账号不能为空' }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
        code: [{ required: true, trigger: 'blur', message: '验证码不能为空' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      copyright:'',
      login_background:'',
      site_name:title,
      captchatImg:'',
      image_left:'',
    }
  },
  created() {
    login_get().then(res => {
      this.copyright=res.data.copyright
      this.login_background='background:url('+res.data.login_background+') no-repeat center center;'
      this.image_left=res.data.image_left
    })
  },
  mounted() {
    this.getCaptcha()
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    getCaptcha() {
      get_captcha().then((res) => {
          this.captchatImg = res.data.captcha
          this.loginForm.key = res.data.key
      })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {

      this.$refs['loginForm'].validate((valid) => {
        if (valid) {
          this.$store.dispatch('user/login', this.loginForm).then((res) => {
            if(res.code==9){
              this.$notify({title:'错误提示',message:res.msg,type: 'error',duration:2000})
              this.getCaptcha()
              return;
            }
            this.$notify({title:'温馨提示',message:res.msg,type: 'success',duration:1000})
            setTimeout(() => {
              this.$router.push({ path: '/home' })
            }, 1000)
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">

/* reset element-ui css */
.login-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-size: cover;
  .show-pwd {
    position: absolute;
    right: 6px;
    font-size: 16px;
    cursor: pointer;
    user-select: none;
  }
  .show-captcha{
    position: absolute;
    right: 0px;
    top:4px;
    font-size: 16px;
    cursor: pointer;
    user-select: none;
  }
  .login-main {
    overflow: hidden;
    position: absolute;
    top: 47%;
    left: 50%;
    width: 1000px;
    transform: translate(-50%,-50%);
    border-radius: 12px;
    display: flex;
    .main-left{
      background-color: #e2e2e2;
      height:394px;
      width:400px;
      img{
        width:100%;
        height:100%;
      }
    }


  }
  .login-form {
    padding: 40px 70px 38px;
    background-color: #fff;
    width:500px;
    .center {
      padding-bottom: 15px;
    }
    .el-form-item {
      border: 1px solid #dae1e6;
      margin-bottom: 24px;
      color: #d1dde8;

      .el-input {
        display: inline-block;
        width:100%;
        line-height: 1;
        input {
          height: auto;
          background: transparent;
          border: 0px;
          border-radius: 0px;
          padding: 5px 5px 5px 10px;
          font-size: 14px;
          color: #333;
          line-height: 1;

        }
      }
    }
    .login-tips {
      .el-checkbox {
        color: #9abcda;
        .el-checkbox__label {
          font-size: 12px;
        }
      }
    }
    .login-btn {
      width: 100%;
      padding: 0;
      margin: 10px auto 0;
      font-size: 14px;
      color: #fff;
      line-height: 40px;
      text-align: center;
      border: none;
      border-radius: 16px;
      cursor: pointer;
      background-color: #009688;
    }
  }

}
.footer {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  padding-bottom: 10px;
  font-size: 14px;
  color: #fff;
  line-height: 30px;
  text-align: center;
  text-shadow: #000 0.1em 0.1em 0.1em;
  .padding-5 {
    padding: 5px !important;
  }
}
</style>
